package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by HL on 2015/9/22.
 */
@Repository
public class FpcxDao extends SimpleHibernateTemplate<Jsd, Long> {


    public Page<Jsd> findfpcxList(Integer type, Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize, Long ywgsID) {
        String hql;
        String szt = "3";
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jsd> page = new Page<Jsd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jsdh != null && !jsdh.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jsdh like ? ");
            param.add("%" + jsdh.trim() + "%");
        }
        if (sqrqStart != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq >= ?");
            param.add(sqrqStart);
        }
        if (sqrqEnd != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" sqrq <= ?");
            param.add(sqrqEnd);
        }
        if (glqyId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.id = ? ");
            param.add(glqyId);
        }
        if (zt != null) {
            szt = "" + zt + "";
        }
        if (glqyQc != null && !"".equals(glqyQc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" glqy.qc like ? ");
            param.add("%" + glqyQc.trim() + "%");
        }
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.id = ? ");
            param.add(gsId);
        }

        if (gsJc != null && !"".equals(gsJc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gs.jc like ? ");
            param.add("%" + gsJc.trim() + "%");
        }
        if (fpzlId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" fpzl = ? ");
            param.add(fpzlId);
        }
        if (type != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" type = ? ");
            param.add(type);
        }

        if (condition.length() != 0)
            condition.append(" and ");
        condition.append(" zt in (" + szt + ") ");
        //==============记录数量===============
        hql = "select count(*) from Jsd where type in (1,2) " + RegionDao.createFpRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果=============== 销售结算和物流结算
        hql = "from Jsd where type in (1,2) " + RegionDao.createFpRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition + "order by zt asc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


}
